#!/bin/bash

# ----------------------------------------------------------------------
# | Navigation                                                         |
# ----------------------------------------------------------------------

alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias cd..='cd ..'

# ----------------------------------------------------------------------
# | Network                                                            |
# ----------------------------------------------------------------------

alias ip='dig +short myip.opendns.com @resolver1.opendns.com'

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

if [ "$OS" == "osx" ]; then
    alias local-ip='ipconfig getifaddr en1'
elif [ "$OS" == "ubuntu" ]; then
    alias local-ip="ifconfig | \
                    | grep 'inet addr' \
                    | grep -v '127.0.0.1' \
                    | cut -d: -f2 \
                    | cut -d' ' -f1"
fi

# ----------------------------------------------------------------------
# | Package Managers                                                   |
# ----------------------------------------------------------------------

# npm
# https://docs.npmjs.com/

if command -v 'npm' &> /dev/null; then
    alias npmi='npm install'
    alias npmr='npm uninstall'
    alias npms='npm search'
    alias npmu='npm update'
fi

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# OS specific package managers

if [ "$OS" == "osx" ]; then

    # Homebrew
    if command -v 'brew' &> /dev/null; then
        alias brewd='brew doctor'
        alias brewi='brew install'
        alias brewr='brew uninstall'
        alias brews='brew search'
        alias brewu='brew update \
                      && brew upgrade --all \
                      && brew cleanup'
    fi

elif [ "$OS" == "ubuntu" ]; then

    # Advanced Packaging Tool
    alias apti='sudo apt-get install'
    alias aptr='sudo apt-get remove'
    alias apts='sudo apt-cache search'
    alias aptu='sudo apt-get update \
                 && sudo apt-get upgrade'

fi

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Update

if [ "$OS" == "osx" ]; then

    alias update='sudo softwareupdate --install --all \
                   && brew update \
                   && brew upgrade --all \
                   && brew cleanup \
                   && npm install -g npm \
                   && npm update -g'

elif [ "$OS" == "ubuntu" ]; then

    alias update='sudo apt-get update \
                   && sudo apt-get upgrade \
                   && npm install -g npm \
                   && npm upgrade -g'

fi

# ----------------------------------------------------------------------
# | Shorter Commands                                                   |
# ----------------------------------------------------------------------

alias :q='exit'
alias c='clear'
alias ch='history -c && > ~/.bash_history'
alias e='vim'
alias g='git'
alias ll='ls -l'
alias m='man'
alias map='xargs -n1'
alias q='exit'
alias rm='rm -rf'

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Copy to clipboard

if [ "$OS" == "osx" ]; then
    alias cc='pbcopy'

elif [ "$OS" == "ubuntu" ]; then
    alias cc='xclip -selection clip'

fi

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Empty Trash

if [ "$OS" == "osx" ]; then

    # Empty the trash, the main HDD and on all mounted volumes
    # and clear Apple’s system logs to improve shell startup speed

    alias empty-trash='sudo rm -frv /Volumes/*/.Trashes \
                        && sudo rm -frv ~/.Trash \
                        && sudo rm -frv /private/var/log/asl/*.asl'

fi

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Hide/Show files

if [ "$OS" == "osx" ]; then

    # Hide/Show desktop icons
    alias hide-desktop-icons='defaults write com.apple.finder CreateDesktop -bool false \
                                && killall Finder'
    alias show-desktop-icons='defaults write com.apple.finder CreateDesktop -bool true \
                                && killall Finder'

    # Hide/Show hidden files in Finder
    alias hide-hidden-files='defaults write com.apple.finder AppleShowAllFiles -bool false \
                                && killall Finder'
    alias show-hidden-files='defaults write com.apple.finder AppleShowAllFiles -bool true \
                                && killall Finder'

elif [ "$OS" == "ubuntu" ]; then

    # Hide/Show desktop icons
    alias hide-desktop-icons='gsettings set org.gnome.desktop.background show-desktop-icons false'
    alias show-desktop-icons='gsettings set org.gnome.desktop.background show-desktop-icons true'

fi

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Lock screen

if [ "$OS" == "osx" ]; then
    alias afk='/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend'

elif [ "$OS" == "ubuntu" ]; then
    alias akf='gnome-screensaver-command --lock'

fi

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Open

if [ "$OS" == "osx" ]; then
    alias o='open'

elif [ "$OS" == "ubuntu" ]; then
    alias o='xdg-open'

fi
